﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HotelProject
{
    public partial class Book : System.Web.UI.Page
    {
        //private DateTime checkInDate;
        //private DateTime checkOutDate;
        private HotelSearchQuery searchQuery;
        private String roomToBookAsString;
        private hotelRoom roomToBook;
        private int roomID;
        private registeredUser currentUser;

        private MySQLDatabaseConnector db;

        protected void Page_Load(object sender, EventArgs e)
        {
            currentUser = Session["currentUser"] as registeredUser;
            if (currentUser == null)
            {
                Response.Redirect("Login.aspx");
            }

            parseBookingData();
        }



        private void parseBookingData()
        {
            if (Page.Request["RoomID"] != null)
            {
                roomToBookAsString = Page.Request["RoomID"] as String;
                bookRoomButton.Text += " " + roomToBookAsString;

                searchQuery = Session["searchQuery"] as HotelSearchQuery;

                if (searchQuery.checkInDate == null || searchQuery.checkOutDate == null || roomToBookAsString == "")
                {
                    bookRoomButton.Enabled = false;
                }

                else
                {
                    db = new MySQLDatabaseConnector();
                    roomID = int.Parse(Page.Request["RoomID"] as string);
                    roomToBook = db.GetRoom(roomID);

                    content.InnerHtml += "Howdy! " + currentUser.userFirstName + "<br/>";
                    content.InnerHtml += roomToBook.ToStringForHTML();
                    content.InnerHtml += "<br/><br/> Check In: " + searchQuery.checkInDate.ToShortDateString() ;
                    content.InnerHtml += "<br/> Check Out: " + searchQuery.checkOutDate.ToShortDateString();
                    content.InnerHtml += "<br/> Total Number Of Days: " + searchQuery.numberOfDays;
                    content.InnerHtml += "<br/> Total Cost: $" + roomToBook.price * searchQuery.numberOfDays;

                }
            }
        }

        protected void bookRoomButton_Click(object sender, EventArgs e)
        {
            Booking newReservation = new Booking() {
                roomID = roomToBook.roomID,
                startDate = searchQuery.checkInDate,
                endDate = searchQuery.checkOutDate,
                hotelID = roomToBook.hotelID,
                totalCost = (int)roomToBook.price * searchQuery.numberOfDays,
                userName = currentUser.userName
            };
            db.addBooking(newReservation);
            Session["booking"] = newReservation;

            db.saveChanges();
            Response.Redirect("ThanksForBooking.aspx");
        }
    }
}